<?php
namespace app\protocol\commands;

use app\protocol\Router;
use Workerman\Connection\TcpConnection;

class HelpCommand extends BaseCommand
{
    protected $router;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function execute($params, TcpConnection $connection): string
    {
        $args = $this->parseParams($params);

        if (count($args) === 1) {
            $command = $args[0];
            $detail = $this->router->getCommandDetail($command);
            if ($detail) {
                return $detail;
            } else {
                throw new \InvalidArgumentException("未知命令: '{$command}'");
            }
        }

        return $this->router->getHelp();
    }

    public static function getDescription(): string
    {
        return '显示帮助信息: help [command]';
    }

    public static function getUsage(): string
    {
        return "help 或 help mul";
    }
}